package com.wangfei.lecture.fanweiyunsuanfu;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamAPITest {

    @Test
    public void test() {
//        ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(HelloWorld.class);
        List<Employee> list = EmployeeData.getEmployees();
        Stream<Employee> stream = list.stream();
        //filter(Predicate p)——接收 Lambda ， 从流中排除某些元素。
        //练习：查询员工表中薪资大于7000的员工信息
        stream.filter(e -> e.getSalary() > 7000).forEach(System.out::println);

        System.out.println("++++++++++++++++++++");

        // limit(n) - 截断流
        list.stream().limit(3).forEach(System.out::println);

        System.out.println("++++++++++++++++++++");

        // skip(n)
        list.stream().skip(3).forEach(System.out::println);

        System.out.println("++++++++++++++++++++");

        // distinct()——筛选，通过流所生成元素的 hashCode() 和 equals() 去除重复元素
        list.add(new Employee(1010,"刘强东",40,8000));
        list.add(new Employee(1010,"刘强东",41,8000));
        list.add(new Employee(1010,"刘强东",40,8000));
        list.add(new Employee(1010,"刘强东",42,8000));
        list.add(new Employee(1010,"刘强东",40,8000));

        list.stream().distinct().forEach(System.out::println);
    }

    @Test
    public void test1() {
        List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
        list.stream().map(str -> str.toUpperCase()).forEach(System.out::println);

        // map下面使用fromStringToStream
        // list.stream()会生成一个stream<String>,但是里面的每一个stream，又通过fromStringToSteam再生成steam，最终的结果就是Stream<Stream
        Stream <Stream <Character>> streamStream = list.stream().map(StreamAPITest::fromStringToStream);
        streamStream.forEach(s->s.forEach(System.out::println));//遍历时候需要双层for循环
        System.out.println();

        //练习3 flatMap下面使用fromStringToStream
        // flatMap(Function f)——接收一个函数作为参数，将流中的每个值都换成另一个流，然后把所有流连接成一个流。
        Stream <Character> characterStream = list.stream().flatMap(StreamAPITest::fromStringToStream);
        characterStream.forEach(System.out::println);

        //super只能添加Father和Father的子类，不能添加Father的父类,读取出来的东西只能存放在Object类里
//        List<? super Father> listSuper = new ArrayList<>();
//        listSuper.add(new Father());
//        listSuper.add(new Human());//compile error
//        listSuper.add(new Son());
//        Father person1 = listSuper.get(0);//compile error
//        Son son = listSuper.get(0);//compile error
//        Object object1 = listSuper.get(0);


    }
    class Human{
    }
    class Father extends Human{
    }
    class Son extends Father{
    }
    class LeiFeng extends Father {
    }
    //将字符串中的多个字符构成的集合转换成对应的Strean的实例
    public static Stream<Character> fromStringToStream(String str){
        List <Character> list = new ArrayList<>();
        for (char c : str.toCharArray()) {
            list.add(c);
        }
        return list.stream();
    }

}
